#include "Lcd.h"
#include "stm32f10x.h"
#include "string.h"
#include "shell.h"
#include "drv.h"

/*void Lcd_Nc(void)
{
}*/

void Lcd_reset(void)
{
    LCD_RST_ENABLE;
    Delay(10);
    LCD_RST_DISABLE;
    GPIOB->BRR = LCD_DIR2;
    LCD_EN_DISABLE;
}

void Lcd_IO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    GPIO_InitStructure.GPIO_Pin = LCD_EN|LCD_RS;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = LCD_RW|LCD_RST|LCD_CS|LCD_DIR2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = LCD_DIR1;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    //write data
    GPIO_InitStructure.GPIO_Pin =GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
}

unsigned char Check_Lcd_Busy(void)
{
  unsigned char i,j;
  unsigned int Temp;  
  
  LCD_Data_Port->CRH=0x44444444; 
  LCD_MCU; 
  
  /*for(j=0;j<250;j++)
  {
    LCD_RS_CMD;
    LCD_RW_RD;
    for(i=0;i<10;i++);
    LCD_EN_ENABLE;
    for(i=0;i<10;i++);
    Temp = LCD_Data_Port->IDR;
//    DEBUG_PRINT("LCD_STATUS = %x \r\n",Temp);
    LCD_EN_DISABLE;
    if((Temp & 0x8000)==0)
    {
      break;
    }
  }
  MCU_LCD;  
  LCD_Data_Port->CRH=0x33333333;
  if(j >= 250) 
  {
    DEBUG_PRINT("LCD BUSY\r\n");
    return 1;
  }
  return 0;*/
  
  for(j=0;j<250;j++)
  {
    LCD_RS_CMD;
    LCD_RW_RD;
    for(i=0;i<10;i++);
    LCD_EN_ENABLE;
    for(i=0;i<10;i++);
    Temp = LCD_Data_Port->IDR;
//    DEBUG_PRINT("LCD_STATUS = %x \r\n",Temp);
    LCD_EN_DISABLE;
    if((Temp & 0x8000)==0)
    {
      MCU_LCD;  
      LCD_Data_Port->CRH=0x33333333;
      return 0;
    }
  }
  DEBUG_PRINT("LCD BUSY\r\n");
  return 1; 
}

void Write_Lcd(unsigned char dat_comm, unsigned char content)
{
  unsigned char i;
  unsigned int temp;
  
  while(Check_Lcd_Busy());
  temp = content;
  temp = temp<<8;
//  DEBUG_PRINT("write data = %x \r\n",temp);
  if(dat_comm) 
    LCD_RS_DATA;
  else  
    LCD_RS_CMD;
  LCD_RW_WR;
  LCD_Data_Port->ODR = temp;
  for(i=0;i<10;i++);
  LCD_EN_ENABLE;
  for(i=0;i<10;i++);
  LCD_EN_DISABLE;
}

unsigned char Read_Lcd(void)
{
  unsigned char i,Read_Data;
  unsigned int temp;
  
  while(Check_Lcd_Busy());
  LCD_Data_Port->CRH=0x44444444; 
  LCD_MCU;
  LCD_RS_DATA;
  LCD_RW_RD;
  for(i=0;i<10;i++);
  LCD_EN_ENABLE;
  for(i=0;i<10;i++);
  temp = LCD_Data_Port->IDR;
  LCD_EN_DISABLE;
  MCU_LCD;
  LCD_Data_Port->CRH=0x33333333;
  Read_Data = temp>>8;
  
  return Read_Data;
}

void Read_Status(void)
{
  unsigned char i,j;
  unsigned int Temp;
  
  LCD_Data_Port->CRH=0x44444444; 
  LCD_MCU; 
  for(j=0;j<250;j++)
  {
    LCD_RS_CMD;
    LCD_RW_RD;
    for(i=0;i<10;i++);
    LCD_EN_ENABLE;
    for(i=0;i<10;i++);
    Temp = LCD_Data_Port->IDR;
//    DEBUG_PRINT("LCD_STATUS = %x \r\n",Temp);
    LCD_EN_DISABLE;
    if((Temp & 0x8000)==0)
    {
      break;
    }
  }
  MCU_LCD;  
  LCD_Data_Port->CRH=0x33333333;
}

void Lcd_Disp_OneByte(unsigned char Byte,unsigned char Row,unsigned char Col)
{
  unsigned char Left_Right,Actual_Col;

  
  Left_Right = Col>>6;
  Actual_Col = Col%64;
  
  if(Left_Right > 0)
    LCD_CS_RIGHT;
  else
    LCD_CS_LEFT;
//  Write_Lcd(0,0x3E);
//  Read_Status();
  Write_Lcd(0,0x3F);
//  Read_Status();
  Write_Lcd(0,Row+0xB8);
  Write_Lcd(0,0xC0);
  Write_Lcd(0,Actual_Col+0x40);
  //for(i=0;i<64;i++)
  Write_Lcd(1,Byte);
  
 
}